import {Util} from "@lowcode/jpaas-common-lib"

import {I18Parse,CommonLang} from "@lowcode/jpaas-common-lib";
import js from '../../locales/lang/impl/assets/jsLang';
let lang = js;
let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang.js.DialogBox,langKey, text)
}
export default {
  props: ["pkId", "layerid", "destroy"],
  data(){
    return {
      labelCol: { span:6 },
      wrapperCol: { span:18 },
      labelCol1: { span:3 },
      wrapperCol1: { span:19 },
      confirmLoading: false,
      mdl:{},
      resultData:{},
      uid:0,
    }
  },
  created(){
    this.loadData();
  },
  methods:{
    //编辑
    edit(key,table) {
      const newData = [...this.mdl[table]];
      const target = newData.filter(item => key === item.uid)[0];
      if (target) {
        target.editable = true;
        this.mdl[table] = newData;
      }
    },

    //页面数据保存
    saveChild(key,table) {
      const newData = [...this.mdl[table]];
      const target = newData.filter(item => key === item.uid)[0];
      delete target.editable;
      this.mdl[table] = newData;
    },
    //增加新数据
    handleAdd(table) {
      var tableData=this.mdl[table];
      const newData = this.subTable[table].row;
      newData.uid = ++this.uid;
      if(!tableData){
        tableData = [];
      }
      tableData.push(Util.deepClone(newData));
      this.$set(this.mdl,table,tableData);
    },
    //编辑时存取数据
    handleChange(value, key, column,table) {
      const newData = [...this.mdl[table]];
      const target = newData.filter(item => key === item.uid)[0];
      if (target) {
        target[column] = value;
        this.mdl[table] = newData;
      }
    },

    //删除
    onDelete(key,table){
      const sonData = this.mdl[table];
      this.mdl[table] = sonData.filter(item => item.uid !== key);
    },

    handSubTableUid(){
      for(var table in this.subTable){
        var rows = this.mdl[table];
        for(var i = 0; i < rows.length; i ++){
          rows[i].uid = this.uid;
          this.uid++;
        }
      }
    },

    loadData(){
      if(!this.pkId){
        if(this.onload_){
          this.onload_()
        }
        return;
      }
      this.confirmLoading = true;
      this.get(this.pkId).then (res => {
        this.confirmLoading = false;
        if(res.success && res.code==200) {
          var data=res.data;

          this.initJson(data);
          if(this.onload_){
            this.onload_(data)
          }
        }
      })
    },

    /**
     * 在提交时将json 数据分解为两个字段。
     * @param row         行数据
     * @param field       字段名称
     * @param textField   文本字段
     * @param valField    值字段
     */
    parseJson(row,field,textField,valField){
      var val=row[field];
      if(!val){
        return ;
      }
      var json=JSON.parse(val);
      delete  row[field];
      if(textField){
        row[textField]=json.text;
      }
      if(valField){
        row[valField]=json.value;
      }
    },
    /**
     * 在加载数据时将记录促成 json 数据。
     * @param row           一行数据
     * @param form          需要构建的目标数据
     * @param textField     文本字段
     * @param valField      值字段
     */
    toJson(row,form,textField,valField){
      var text=row[textField];
      var val=row[valField];
      var json={text:text,value:val};
      row[form]=JSON.stringify(json);
    },

    initJson(json){

      this.mdl=json;
      this.handSubTableUid();

      if(!this.complexJson){
        return;
      }
      var main=this.complexJson.main;
      for(var key in main){
        var obj=main[key];
        this.toJson(json,key,obj.text,obj.value);
      }

      //处理子表。
      for(var table in this.complexJson){
        if(table!="main"){
          continue;
        }
        var tableKey="sub__" + table;
        var tableData=json[tableKey];
        if(!tableData){
          if(!sub){
            continue;
          }
        }
        var sub=this.complexJson[table];
        if(!sub){
          continue;
        }
        for(var i=0;i<tableData.length;i++){
          var row=tableData[i];
          for(var key in sub){
            var obj=sub[key];
            this.toJson(row,key,obj.text,obj.value);
          }
        }
      }
    },
    cancel(){
      Util.closeWindow(this,"cancel");
    },
    onSubmit(json){
      /*
      {value:aa,text:""}
      * main:{user:{text:"",value:""}},
      * table:{user:{text:"",value:""}}
      * */
      if(!this.complexJson){
        return;
      }
      var main=this.complexJson.main;
      for(var key in main){
        var obj=main[key];
        this.parseJson(json,key,obj.text,obj.value);
      }
      //处理子表。
      for(var table in this.complexJson){
        if(table!="main"){
          continue;
        }
        var tableKey="sub__" + table;
        var tableData=json[tableKey];
        if(!tableData){
          continue;
        }
        var sub=this.complexJson[table];
        if(!sub){
          continue;
        }
        for(var i=0;i<tableData.length;i++){
          var row=tableData[i];
          for(var key in sub){
            var obj=sub[key];
            this.parseJson(row,key,obj.text,obj.value);
          }
        }
      }
    },
    handleSubmit (formName) {
      this.$refs.form.validate(valid => {
        if(!valid) {
          return;
        }
        
        this.mdl.pkId = this.pkId;
        this.confirmLoading = true;
        if(this.validForm){
          var flag=this.validForm(this.mdl);
          if(!flag){
            return;
          }
        }
        
          this.onSubmit(this.mdl);
          this.save(this.mdl).then(res => {
              if (res.success && res.code == 200) {
                //返回结果数据。
                this.resultData = res.data;
                Util.closeWindow(this, "ok");
              }
            }
          ).catch(() => {
            this.$message.error(I18Parse.getI18Val(CommonLang,'rxMsa.sysError','系统错误，请稍后再试!'))
          }).finally(() => {
            this.confirmLoading = false
          })
      });
    }
  }
}